package client

import (
	"context"
	"google.golang.org/grpc"
	takeout "takeout-ordering/common/proto"
)

type Handler func(ctx context.Context, in takeout.TakeoutClient) (interface{}, error)

func TakeoutClient(ctx context.Context, handler Handler) (interface{}, error) {
	dial, err := grpc.Dial("127.0.0.1:8888", grpc.WithInsecure())
	if err != nil {
		return nil, err
	}
	client := takeout.NewTakeoutClient(dial)
	res, err := handler(ctx, client)
	if err != nil {
		return nil, err
	}
	defer dial.Close()
	return res, nil
}

func UserRegister(ctx context.Context, i *takeout.UserRegisterReq) (*takeout.UserRegisterResp, error) {
	client, err := TakeoutClient(ctx, func(ctx context.Context, in takeout.TakeoutClient) (interface{}, error) {
		register, err := in.UserRegister(ctx, i)
		if err != nil {
			return nil, err
		}
		return register, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*takeout.UserRegisterResp), nil
}
func UserLogin(ctx context.Context, i *takeout.UserLoginReq) (*takeout.UserLoginResp, error) {
	client, err := TakeoutClient(ctx, func(ctx context.Context, in takeout.TakeoutClient) (interface{}, error) {
		register, err := in.UserLogin(ctx, i)
		if err != nil {
			return nil, err
		}
		return register, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*takeout.UserLoginResp), nil
}
func MealsList(ctx context.Context, i *takeout.MealsListReq) (*takeout.MealsListResp, error) {
	client, err := TakeoutClient(ctx, func(ctx context.Context, in takeout.TakeoutClient) (interface{}, error) {
		register, err := in.MealsList(ctx, i)
		if err != nil {
			return nil, err
		}
		return register, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*takeout.MealsListResp), nil
}
func CreateOrder(ctx context.Context, i *takeout.CreateOrderReq) (*takeout.CreateOrderResp, error) {
	client, err := TakeoutClient(ctx, func(ctx context.Context, in takeout.TakeoutClient) (interface{}, error) {
		register, err := in.CreateOrder(ctx, i)
		if err != nil {
			return nil, err
		}
		return register, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*takeout.CreateOrderResp), nil
}

func OrderPay(ctx context.Context, i *takeout.OrderPayReq) (*takeout.OrderPayResp, error) {
	client, err := TakeoutClient(ctx, func(ctx context.Context, in takeout.TakeoutClient) (interface{}, error) {
		register, err := in.OrderPay(ctx, i)
		if err != nil {
			return nil, err
		}
		return register, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*takeout.OrderPayResp), nil
}
func Payback(ctx context.Context, i *takeout.PaybackReq) (*takeout.PaybackResp, error) {
	client, err := TakeoutClient(ctx, func(ctx context.Context, in takeout.TakeoutClient) (interface{}, error) {
		register, err := in.Payback(ctx, i)
		if err != nil {
			return nil, err
		}
		return register, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*takeout.PaybackResp), nil
}

func FindCoupon(ctx context.Context, i *takeout.FindCouponReq) (*takeout.FindCouponResp, error) {
	client, err := TakeoutClient(ctx, func(ctx context.Context, in takeout.TakeoutClient) (interface{}, error) {
		register, err := in.FindCoupon(ctx, i)
		if err != nil {
			return nil, err
		}
		return register, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*takeout.FindCouponResp), nil
}

func UserOrderList(ctx context.Context, i *takeout.UserOrderListReq) (*takeout.UserOrderListResp, error) {
	client, err := TakeoutClient(ctx, func(ctx context.Context, in takeout.TakeoutClient) (interface{}, error) {
		register, err := in.UserOrderList(ctx, i)
		if err != nil {
			return nil, err
		}
		return register, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*takeout.UserOrderListResp), nil
}

func CouPonReceive(ctx context.Context, i *takeout.CouPonReceiveReq) (*takeout.CouPonReceiveResp, error) {
	client, err := TakeoutClient(ctx, func(ctx context.Context, in takeout.TakeoutClient) (interface{}, error) {
		register, err := in.CouPonReceive(ctx, i)
		if err != nil {
			return nil, err
		}
		return register, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*takeout.CouPonReceiveResp), nil
}
